package game

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"go.mongodb.org/mongo-driver/bson"
	"higame/modules/log"
	"higame/modules/request"
	"higame/modules/utils"
	"net/http"
	"strings"
)

func create(ctx *gin.Context) {
	gameSchema, err := bindData(ctx)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	// 检测是否已经创建了相同的游戏
	if _, err = FindByCode(gameSchema.Code); err == nil {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "已经创建了相同的游戏!",
			"data": nil,
		})
		return
	}

	// 添加游戏
	err = AddOne(gameSchema)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"code": http.StatusInternalServerError,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "添加成功",
		"data": gameSchema,
	})
}

func delete(ctx *gin.Context) {
	gameSchema, err := bindID(ctx)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	err = DeleteByID(gameSchema.ID)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"code": http.StatusInternalServerError,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "删除成功",
		"data": nil,
	})

}

func update(ctx *gin.Context) {
	gameSchema, err := bindData(ctx)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	err = UpdateByCode(gameSchema.Code, bson.M{
		"name":      gameSchema.Name,
		"status":    gameSchema.Status,
		"version":   gameSchema.Version,
		"down_url":  gameSchema.DownUrl,
		"is_up":     gameSchema.IsUp,
		"notice":    gameSchema.Notice,
		"up_log":    gameSchema.UpLog,
		"icon":      gameSchema.Icon,
		"home_page": gameSchema.HomePage,
		"app_name":  gameSchema.AppName,
	})
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "修改成功",
		"data": nil,
	})
}

func get(ctx *gin.Context) {
	gameSchema, err := bindID(ctx)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	g, err := FindByID(gameSchema.ID)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"code": http.StatusInternalServerError,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "查询成功",
		"data": g,
	})
}

func list(ctx *gin.Context) {
	pageNum := utils.StringToInt64(ctx.PostForm("page_num"), 1) - 1
	pageSize := utils.StringToInt64(ctx.PostForm("page_size"), 20)
	keyword := ctx.PostForm("keyword")
	if pageNum < 0 {
		pageNum = 0
	}
	if pageSize > 100 {
		pageSize = 20
	}
	list, _ := FindAll(pageNum, pageSize, bson.M{
		"name": bson.M{
			"$regex":   strings.Trim(keyword, ""),
			"$options": "$i",
		},
	})
	count, _ := Count(bson.M{
		"name": bson.M{
			"$regex":   strings.Trim(keyword, ""),
			"$options": "$i",
		},
	})

	var str = ""
	if count == 0 {
		str = "没有查询到数据！"
	} else {
		str = utils.JoinString("共%v页%v行 当前在第%v页", count/pageSize+1, count, pageNum+1)
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  str,
		"data": gin.H{
			"list":  list,
			"total": count,
		},
	})
}

func generateCode(ctx *gin.Context) {
	ctx.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "生成成功!",
		"data": utils.MakeKey(12),
	})

}

// 搜索游戏资料
func search(ctx *gin.Context) {
	gameName := ctx.PostForm("game_name")

	sourceUrl := "https://www.douban.com/j/ilmen/game/search?genres=&platforms=&q=" + myhttp.MustEncode(gameName) + "&sort=rating"
	res, err := myhttp.Get(sourceUrl)

	var resJson interface{}
	err = json.Unmarshal([]byte(res), &resJson)

	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"code": http.StatusInternalServerError,
			"msg":  "服务器错误！",
			"data": nil,
		})
		log.Error.Printf("%v", err)
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "成功！",
		"data": resJson,
	})
}
